package cn.snmp.mutual.uk.stack;

import java.util.*;
import java.io.*;

class AsnEncoderv1 extends AsnEncoderBase {
    private static final String version_id = "@(#)$Id: AsnEncoderv1.java,v 3.3 2006/02/09 14:16:36 birgit Exp $ Copyright Westhawk Ltd";

    ByteArrayOutputStream EncodeSNMP(final SnmpContext context, final byte msg_type, final String enterprise, final byte[] IpAddress, final int generic_trap, final int specific_trap, final long timeTicks, final Enumeration ve) throws IOException, EncodingException {
        final AsnSequence asnTopSeq = new AsnSequence();
        asnTopSeq.add(new AsnInteger(0));
        asnTopSeq.add(new AsnOctets(context.getCommunity()));
        final AsnObject asnPduObject = this.EncodeTrap1Pdu(msg_type, enterprise, IpAddress, generic_trap, specific_trap, timeTicks, ve);
        asnTopSeq.add(asnPduObject);
        if (AsnObject.debug > 10) {
            System.out.println("\n" + this.getClass().getName() + ".EncodeSNMP(): ");
        }
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        asnTopSeq.write(bout);
        return bout;
    }

    private AsnObject EncodeTrap1Pdu(final byte msg_type, final String enterprise, final byte[] IpAddress, final int generic_trap, final int specific_trap, final long timeTicks, final Enumeration ve) throws IOException {
        final AsnObject asnPduObject = new AsnSequence(msg_type);
        asnPduObject.add(new AsnObjectId(enterprise));
        asnPduObject.add(new AsnOctets(IpAddress, (byte) 64));
        asnPduObject.add(new AsnInteger(generic_trap));
        asnPduObject.add(new AsnInteger(specific_trap));
        asnPduObject.add(new AsnUnsInteger(timeTicks));
        final AsnObject asnVBLObject = asnPduObject.add(new AsnSequence());
        while (ve.hasMoreElements()) {
            final AsnObject asnVBObject = asnVBLObject.add(new AsnSequence());
            final Varbind vb = (Varbind) ve.nextElement();
            asnVBObject.add(vb.getOid());
            asnVBObject.add(vb.getValue());
        }
        return asnPduObject;
    }

    ByteArrayOutputStream EncodeSNMP(final SnmpContext context, final byte msg_type, final int pduId, final int errstat, final int errind, final Enumeration ve) throws IOException, EncodingException {
        final AsnSequence asnTopSeq = new AsnSequence();
        asnTopSeq.add(new AsnInteger(0));
        asnTopSeq.add(new AsnOctets(context.getCommunity()));
        final AsnObject asnPduObject = this.EncodePdu(msg_type, pduId, errstat, errind, ve);
        asnTopSeq.add(asnPduObject);
        if (AsnObject.debug > 10) {
            System.out.println("\n" + this.getClass().getName() + ".EncodeSNMP(): ");
        }
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        asnTopSeq.write(bout);
        return bout;
    }
}
